﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;

public partial class Manage_Cms_IDCardValidate : System.Web.UI.Page
{
    #region 定义相关变量
    protected KeRui.BLL.Member bllMember = new KeRui.BLL.Member();
    protected KeRui.BLL.Seller bllSeller = new KeRui.BLL.Seller();
    protected KeRui.BLL.Validate bllValidate = new KeRui.BLL.Validate();
    protected KeRui.BLL.CmsComment bllComment = new KeRui.BLL.CmsComment();
    protected KeRui.BLL.IntegralDetails bllIntegralDetail = new KeRui.BLL.IntegralDetails();
    protected KeRui.Model.IntegralDetails modelIntegralDetail = new KeRui.Model.IntegralDetails();
    protected KeRui.Model.Member modelMember = new KeRui.Model.Member();
    protected KeRui.Model.Seller modelSeller = new KeRui.Model.Seller();
    protected KeRui.Model.Validate modelValidate = new KeRui.Model.Validate();
    protected KeRui.Model.CmsComment modelComment = new KeRui.Model.CmsComment();
    protected int ValidateID = PageRequest.GetInt("id", 0);
    protected string UserName = PageRequest.GetString("uname");
    protected int SenderID = 0;     //系统后台登录者ID
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        ManageHelper.CheckAdminLogin();//登陆判断

        SenderID = int.Parse(Request.Cookies["account"].Values["userid"].ToString());
        
        #region 加载用户信息
        int UserID = PageRequest.GetInt("uid", 0);
        modelMember = bllMember.GetModel(UserID);
        modelSeller = bllSeller.GetModel(bllSeller.GetSellerIDByUserid(UserID));
        if (modelMember == null || modelSeller == null)
        {
            modelMember = new KeRui.Model.Member();
            modelSeller = new KeRui.Model.Seller();
        }
        #endregion

        if (ManageHelper.PageAct() == "validate")
        {
            IDCardValidate();
        }
    }

    #region 实名认证
    private void IDCardValidate()
    {
        string strValidate = "";
        modelValidate.State = (PageRequest.GetString("rdoCheck") == "是" ? 2 : 1);
        modelValidate.Id = PageRequest.GetInt("ValidateID",0);
        modelValidate.TransactTime = DateTime.Parse(DateTime.Now.ToString());
        bllValidate.Update(modelValidate);
        if (modelValidate.State == 1)
        {
            strValidate = "认证未通过";
            SendValidateEmail("注意，您所提交的认证信息与实际情况不符，请重新认证！");
        }
        else
        {
            Presented(modelValidate);   //实名验证成功后赠送5个积分
            strValidate = "认证通过";
            SendValidateEmail("恭喜，您的实名认证已通过！");
        }
        Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"IDCardValidateList.aspx?sucmsg=" + strValidate + "\";</script>");    
        Response.End();
    }
    #endregion

    #region 认证后给用户发送一条站内信息
    /// <summary>
    /// 认证后给用户发送一条信息
    /// </summary>
    private void SendValidateEmail(string strContent)
    {
        modelComment.PublicID = SenderID; //PublicID字段做为分类信息中商户消息的发送者ID
        modelComment.UserID = PageRequest.GetInt("uid",0);       //UserID字段做为分类信息商户消息中的接收者ID
        modelComment.Username = PageRequest.GetString("UName");    //Username字段做为分类信息中商户消息的接收者
        modelComment.City = "实名认证中心";     //City字段做为分类信息中商户消息的标题
        modelComment.Content = strContent;    //Content字段做为分类信息中商户消息的内容
        modelComment.CreateIP = PageRequest.GetIP();
        bllComment.Add2(modelComment);
    }
    #endregion

    #region 实名验证成功后赠送5个积分
    /// <summary>
    /// 实名验证成功后赠送5个积分
    /// </summary>
    /// <param name="modelValidate">验证信息表</param>
    private void Presented(KeRui.Model.Validate modelValidate)
    {
        #region 修改用户积分明细
        modelIntegralDetail.UserId = modelValidate.UserID;
        modelIntegralDetail.UserName = modelValidate.UserName;
        modelIntegralDetail.Type = 0;
        modelIntegralDetail.Integrals = 5;
        modelIntegralDetail.HandleDate = System.DateTime.Now;
        modelIntegralDetail.Remark = "实名验证成功，系统赠送5个积分以示奖励！";
        bllIntegralDetail.Add(modelIntegralDetail);
        #endregion

        #region 修改用户信息（赠送5点积分）
        modelMember = bllMember.GetModel(modelValidate.UserID);
        if (modelMember != null)
        {
            modelMember.Integral = modelMember.Integral + 5;
            bllMember.Update_AllInfo(modelMember);
        }
        #endregion
    }
    #endregion
}
